<div class="layui-card-header layui-card">
    <span class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>申请代付</cite></a>
    </span>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">批量代付</li>
            </ul>
            <div class="layui-tab-content">
                <form class="layui-form center">
                    <div class="layui-form-item">
                        <blockquote class="layui-elem-quote" id="amountTip"></blockquote>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">代付金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" required  name="agentpayAmount" id="agentpayAmount"  lay-verify="required|number" placeholder="请输入代付金额" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">笔数</label>
                        <div class="layui-input-block">
                            <input type="text" required  name="agentpayNumber" id="agentpayNumber"  lay-verify="required|number" placeholder="请输入代付笔数" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item" id="payAuthDiv" style="display: none">
                        <label class="layui-form-label">支付密码</label>
                        <div class="layui-input-block">
                            <input type="password" required id="payPassword" name="payPassword"  placeholder="请输入支付密码" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item" id="googleAuthDiv" style="display: none">
                        <label class="layui-form-label">谷歌验证码</label>
                        <div class="layui-input-block">
                            <input type="text" required id="googleCode" name="googleCode"  placeholder="请输入谷歌验证码" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <label class="layui-form-label">上传文件</label>
                        <div class="layui-input-block">
                            <button type="button" class="layui-btn layui-btn-normal" id="selectBtn">选择文件</button>
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-block">
                            <span style="color: red">系统会核对代付金额,笔数与上传文件是否一致</span>
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <div class="layui-input-block">
                            <button type="button" lay-submit lay-filter="btnJSJ" class="layui-btn" id="uploadBtn">提交申请</button>
                            <a href="/xxpay_batch_agentpay.xls" class="layui-btn layui-btn-warm">下载模板</a>
                        </div>
                    </div>
                </form>
            </div>


        </div>
    </div>
</div>
<style>
    table{
        text-align: center;
    }
    .leftT {
        text-align: left;
    }
</style>

<script>
    layui.use(['table','util','admin','upload'], function(){
        var form = layui.form
        ,table = layui.table
        ,$ = layui.$
        ,admin = layui.admin
        ,upload = layui.upload
        ,element = layui.element;
        
        element.render('breadcrumb', 'breadcrumb');//渲染导航信息
        var paySecurityType;
        admin.req({
            type: 'get',
            url: layui.setter.baseUrl + '/mch/get',
            error: function(err){
                layer.alert(err.msg,{title:'请求失败'})
            },
            success:function(res){
                if(res.code == 0 && res.data != null){
                    paySecurityType = res.data.paySecurityType;
                    if(paySecurityType == 0) {
                    }else if(paySecurityType == 1) {
                        $("#payAuthDiv").show();
                    }else if(paySecurityType == 2) {
                        $("#googleAuthDiv").show();
                    }else if(paySecurityType == 3) {
                        $("#payAuthDiv").show();
                        $("#googleAuthDiv").show();
                    }
                }
            }
        });
        admin.req({
            type: 'get',
            url: layui.setter.baseUrl + '/account/get',
            error: function(err){
                layer.alert(err.msg,{title:'请求失败'})
            },
            success:function(res){
                if(res.code == 0 && res.data != null){
                    $('#amountTip').html("可结算金额: <span style='color: red'>" + res.data.availableAgentpayBalance / 100 + "</span> 元");
                }
            }
        });

        var uploadInst = upload.render({
            elem: '#selectBtn' //绑定元素
            ,url: layui.setter.baseUrl + '/agentpay/batch_upload/' //上传接口
            ,accept : 'file'
            ,exts : "csv|xls|xlsx"
            ,auto : false
            ,bindAction: '#uploadBtn'
            ,field : "upFile"
            ,data : {
                agentpayAmount: function(){
                    return $("#agentpayAmount").val();
                },
                agentpayNumber: function(){
                    return $("#agentpayNumber").val();
                },
                payPassword: function(){
                    return $("#payPassword").val();
                },
                googleCode: function(){
                    return $("#googleCode").val();
                },
                access_token: layui.data('layuiAdmin').access_token
            }
            ,done: function(res){
                //上传完毕回调
                if(res.code == 0) {
                    var data = res.data;
                    layer.alert(
                            "申请完成,最终结果查看代付列表[代付金额:" + data.totalAmount/100 + "元, 手续费: "+ data.totalFee/100 +"元, 笔数:" + data.batchInertCount + ", 耗时:" + data.costTime/1000+ "秒]"
                            ,{title: "保存成功"}
                            ,function(index){
                        layer.close(index);
                                location.hash = '/agentpay/list';
                    })
                }else {
                    layer.alert("上传失败[" + res.msg + "]");
                }
            }
            ,error: function(){
                //请求异常回调
                layer.alert("上传失败,系统异常");
            }
        });

        form.on('submit(btnJSJ)', function(data){
            // 校验
            var payPassword = $("#payPassword").val();
            var googleCode = $("#googleCode").val();
            if(paySecurityType == 0) {
            }else if(paySecurityType == 1) {
                if(payPassword == '') {
                    layer.alert("请填写支付密码!", function(index){
                        layer.close(index);
                    });
                    return false;
                }
            }else if(paySecurityType == 2) {
                if(googleCode == '') {
                    layer.alert("请填写谷歌验证码!", function(index){
                        layer.close(index);
                    });
                    return false;
                }
            }else if(paySecurityType == 3) {
                if(payPassword == '') {
                    layer.alert("请填写支付密码!", function(index){
                        layer.close(index);
                    });
                    return false;
                }
                if(googleCode == '') {
                    layer.alert("请填写谷歌验证码!", function(index){
                        layer.close(index);
                    });
                    return false;
                }
            }
            return false;//阻止跳转
        });
        form.render();


    });
</script>